//
//  ViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/4/22.
//

import UIKit

import Flutter
import flutter_boost

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        var button = UIButton(type:UIButton.ButtonType.custom)
        button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
        button.setTitle("Show Flutter!", for: UIControl.State.normal)
        button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)
        
        
        button = UIButton(type:UIButton.ButtonType.custom)
        button.addTarget(self, action: #selector(showFlutterBoost), for: .touchUpInside)
        button.setTitle("Show Flutter! By Flutter Boost", for: UIControl.State.normal)
        button.frame = CGRect(x: 80.0, y: 310.0, width: 260.0, height: 40.0)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)
        
        
        
        button = UIButton(type:UIButton.ButtonType.custom)
        button.addTarget(self, action: #selector(testFlutterBoost), for: .touchUpInside)
        button.setTitle("test Flutter Boost", for: UIControl.State.normal)
        button.frame = CGRect(x: 80.0, y: 410.0, width: 260.0, height: 40.0)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)
    }
    
    @objc func showFlutter() {
        let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
        let flutterViewController =
        FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
        print(self.navigationController ?? "is nil")
        
        //present(flutterViewController, animated: true, completion: nil)
        self.navigationController?.pushViewController(flutterViewController, animated: true)
        //self.showDetailViewController(UINavigationController(rootViewController: flutterViewController), sender: nil)
    }
    
    @objc func showFlutterBoost() {
        print("showFlutterBoost >>> ")
        let options = FlutterBoostRouteOptions()
        options.pageName = "debug"
        options.arguments = ["isAnimated" :true]

        //页面是否透明（用于透明弹窗场景），若不设置，默认情况下为true
        options.opaque = false

        //这个是push操作完成的回调，而不是页面关闭的回调！！！！
        options.completion = { completion in
            print("open operation is completed")
        }

        //这个是页面关闭并且返回数据的回调，回调实际需要根据您的Delegate中的popRoute来调用
        options.onPageFinished = { dic in
            print(dic)
        }

        FlutterBoost.instance().open(options)
    }
    
    @objc func testFlutterBoost() {
        let delegate = BoostDelegate()
        print("testFlutterBoost")
        print(delegate.getCurrent())
        print(UIApplication.shared.keyWindow?.rootViewController)
    }
}

